package com.wwz.main;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class DBHelper {  //数据库帮助类
    private static String url;
    private static String username;
    private static String password;

    static {
        init();
    }
    public void close(Connection connection){  //关闭connection
        if(connection!=null){
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    public void close(Statement stmt){  //关闭statement
        if (stmt!=null) {
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }

    public void close(ResultSet rs){//关闭resultset
        if (rs!=null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static void init(){  //加载配置文件
        Properties prop = new Properties();
        InputStream is = DBHelper.class.getClassLoader().getResourceAsStream("D:\\IDEProject\\StuConSys\\src\\com\\wwz\\main\\db.properties");
        try {
            prop.load(is);
            url = prop.getProperty("url");
            username = prop.getProperty("username");
            password = prop.getProperty("password");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
